package com.zretc.controller;

import com.config.ServiceException;
import com.util.CodeMsg;
import com.util.Result;
import com.zretc.pojo.ImageData;
import com.zretc.pojo.User;
import com.zretc.service.ImageDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * Filename:    UploadResource
 * Description: 影像上传控制器
 * Copyright:   Copyright (c) 2018-2019 All Rights Reserved.
 * Company:     chinasofti.com Inc.
 *
 * @author: mazheng
 * @version: 1.0
 * Create at:   2022--8-23
 * <p>
 * Modification History:
 * Date         Author      Version     Description        tag
 * ------------------------------------------------------------------
 * 2022--8-23      mazheng      1.0         1.0 Version        new
 * 2022-9-8        xujingyi     2.0         2.0 Version        new
 */
@RestController
@Api("授信资料")
@ApiOperation("ImageDataController")
public class ImageDataController {
    @Autowired
    private Environment environment;

    /**
     * 单文件接收二进制方式
     * @param file   file文件
     * @param sid    身法证号
     * @param req   请求对象
     * @return
     * @throws ServiceException 业务异常
     */
    @PostMapping("/upload_sx")
    public Result upload_sx(@RequestPart("file") Part  file,@RequestPart String sid, HttpServletRequest req) throws ServiceException {
        String sufix = file.getSubmittedFileName().substring(file.getSubmittedFileName().lastIndexOf("."));
        String path = environment.getProperty("fileupload.path");
        String filename = sid + "@" + UUID.randomUUID().toString() + sufix;
        try {
            FileCopyUtils.copy(file.getInputStream(), new FileOutputStream(new File(path, filename)));
        } catch (IOException e) {
            e.printStackTrace();
            throw new ServiceException("500401", e);
        }
        return Result.success(filename);
    }

    @PostMapping("/upload_sx_all")
    public Result upload_sx(@RequestParam(value = "imgs", required = false) List<String> imgs, String sid) throws ServiceException {

        System.out.println(imgs.size());

        // 返回文件名数组
        return Result.success("xx");
    }

    /**
     * 根据文件名字删除
     * @param filename
     * @param req
     * @return
     * @throws ServiceException
     */
    @GetMapping("/upload_del")
    public Result upload_sx(@RequestParam String filename, HttpServletRequest req) throws ServiceException {


        String path = environment.getProperty("fileupload.path");
        File f = new File(path, filename);
        boolean flg = f.delete();

        return flg ? Result.success("success"): Result.success(CodeMsg.messageprop.get("5004011"));
    }
    @Autowired
    private ImageDataService ImageDataService;

    /**
     * 获取所有客户信息
     * 啊啊啊
     *
     * @param idCard 身份id
     * @return
     */
    @GetMapping("/findImageDataByIdCard")
    public Result<ImageData> findImageDataByIdCard(String idCard) {
        // 根据身份证询客户的图片信息
        ImageData ImageData = ImageDataService.findImageDataByIdCard(idCard);
        /*身份证校验*/
        /*===============================*/
        if (isCard(idCard)) {
            System.out.println("身份证号码正确");
            return Result.success(ImageData);
        } else {
            return null;
        }
        /*=================================*/
        // 返回值
//        return Result.success(ImageData);
    }
    /*=================================*/

    /**
     * 验证身份证真假
     *
     * @param carNumber 身份证号
     * @return boolean
     */
    public static boolean isCard(String carNumber) {
        //判断输入身份证号长度是否合法
        if (carNumber.length() != 18) {
            throw new RuntimeException("身份证长度不合法");//不合法 抛出一个异常
        }
        //校验身份证真假
        int sum = 0;
        int[] weight = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};//将加权因子定义为数组
        //遍历weight数组 求身份证前17项系数和
        for (int i = 0; i < weight.length; i++) {
            int n = carNumber.charAt(i) - 48;//获取 身份证对应数
            int w = weight[i];
            sum += w * n;
        }
        //对11求余
        int index = sum % 11;
        //校验码

        String m = "10X98765432";
        //获取身份证最后一位进行比对
        return m.charAt(index) == carNumber.charAt(17);
    }
    /*=================================*/



}
